package com.tenebrous.jestic.service.framwork.meber.organ;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tenebrous.jestic.entity.framwork.meber.organ.OrgUserGrant;
import com.tenebrous.jestic.mapper.idao.framwork.meber.organ.OrgUserGrantMapper;
import com.tenebrous.jestic.service.SuperService;
import com.rain.ops.sdk.utils.UUIDUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class OrgUserGrantService extends SuperService<OrgUserGrant> {

    @Autowired
    private OrgUserGrantMapper mapper;

    @Override
    @Autowired
    protected void setSuperMapper() {
        super.setSuperMapper(this.mapper);
    }

    public PageInfo pagingList(Integer pageIndex, Integer pageSize, String userid) {
        return PageHelper.startPage(pageIndex, pageSize).doSelectPageInfo(() -> this.mapper.selectByUserid(userid));
    }

    @Transactional
    public int updateGrant(String userid, String targets) {
        int cnt = 0;
        Date dt = new Date();
        JSONArray array = JSON.parseArray(targets);
        for (int i = 0; i < array.size(); i++) {
            JSONObject jobj = array.getJSONObject(i);
            String targetid = jobj.getString("uid");
            OrgUserGrant eitem = this.mapper.selectByUseridAndTargetid(userid, targetid);
            if (eitem == null) {
                OrgUserGrant gitem = new OrgUserGrant();
                gitem.setId(UUIDUtils.randomUUID());
                gitem.setUserid(userid);
                gitem.setGrantTargetid(targetid);
//                gitem.setGrantScope(jobj.getString("scope"));
                gitem.setGrantTargetype(jobj.getString("utype"));
                gitem.setGrantTargetname(jobj.getString("uname"));
                gitem.setSort(dt.getTime() + i);
                this.mapper.insertSelective(gitem);
            }
        }
        return cnt;
    }

    @Transactional
    public int updateGrantScope(String grantid, String[] scopes) {
        OrgUserGrant item = this.mapper.selectByPrimaryKey(grantid);
        if (item != null) {
            item.setGrantScope(StringUtils.join(scopes, ","));
            return this.mapper.updateByPrimaryKey(item);
        }
        return 0;
    }

    @Transactional
    public int deleteByIds(String[] ids) {
        int i = 0;
        for (String id : ids) {
            i += this.mapper.deleteByPrimaryKey(id);
        }
        return i;
    }

    public List<String> checkGrant(String userid, String[] targetids) {
        List<String> list = new ArrayList<>();
        List<String> slist = this.mapper.checkGrant(userid, targetids);
        for (String strs : slist) {
            String[] array = strs.split(",");
            for (String item : array) {
                if (!list.contains(item)) {
                    list.add(item);
                }
            }
        }
        return list;
    }
}
